package com.cetc.nsfx.controller;

import cn.apiclub.captcha.Captcha;
import cn.apiclub.captcha.servlet.CaptchaServletUtil;
import cn.apiclub.captcha.text.producer.DefaultTextProducer;
import cn.apiclub.captcha.text.renderer.DefaultWordRenderer;
import cn.apiclub.captcha.text.renderer.WordRenderer;
import com.cetc.nsfx.util.GlobalKeys;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/24 0024.
 */
@Controller
@RequestMapping("/captcha")
public class CaptchaController {


    @RequestMapping("/makeCaptcha")
    public void makeCaptcha(HttpServletRequest req, HttpServletResponse resp){
        char[] numberChar = new char[] { '1','2', '3', '4', '5', '6', '7', '8','9','0' };
        Captcha.Builder builder=new Captcha.Builder(80, 40);
        //增加边框
        builder.addBorder();

        List<Font> fontList = new ArrayList<Font>();
        fontList.add(new Font("黑体", Font.BOLD, 32));//可以设置斜体之类的


        //加入多种颜色后会随机显示 字体空心
        List<Color> colorList=new ArrayList<Color>();
        colorList.add(Color.black);
        colorList.add(Color.blue);
        colorList.add(Color.green);

        WordRenderer wr = new DefaultWordRenderer(colorList,fontList);

        builder.addText(new DefaultTextProducer(4,numberChar),wr);

        HttpSession session = req.getSession();
        Captcha captcha =  builder .build();

        session.setAttribute(GlobalKeys.SESSION_CAPTCHA, captcha);
        CaptchaServletUtil.writeImage(resp, captcha.getImage());
    }


}
